package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.common.utils.JwtUtil;
import com.company.project.pojo.role.Role;
import com.company.project.pojo.role.RoleDTO;
import com.company.project.pojo.rolePermission.RolePermission;
import com.company.project.pojo.roleVillage.RoleVillage;
import com.company.project.pojo.roleVillage.RoleVillageDTO;
import com.company.project.pojo.village.Village;
import com.company.project.service.IRolePermissionService;
import com.company.project.service.IRoleService;
import com.company.project.service.IRoleVillageService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-10
 */
@Api(value = "角色",tags = "角色")
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IRolePermissionService iRolePermissionService;
    @Autowired
    private IRoleVillageService iRoleVillageService;

    @ApiOperation(value = "获取角色列表")
    @RequestMapping(value = "getRoleList", method = RequestMethod.GET)
    @ResponseBody
    public Rest getRoleList(){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);

        List roleList = iRoleService.list(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , roleList);
    }

    @ApiOperation(value = "获取角色列表分页")
    @RequestMapping(value = "getRoleListPage", method = RequestMethod.GET)
    @ResponseBody
    public Rest getRoleListPage(Integer currentPage, String name){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(name != null && !name.equals("")){
            queryWrapper.like("name", name);
        }
        queryWrapper.eq("is_delete", 0);

        IPage page = iRoleService.page(new Page<>(currentPage, 10), queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , page);
    }

    @ApiOperation(value = "获取角色")
    @RequestMapping(value = "getRoleById", method = RequestMethod.GET)
    @ResponseBody
    public Rest getRoleById(int id){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);

        Role Role = iRoleService.getOne(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , Role);
    }

    @ApiOperation(value = "添加角色")
    @RequestMapping(value = "addRole", method = RequestMethod.POST)
    @ResponseBody
    public Rest addRole(@RequestBody Role role){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", role.getName());
        queryWrapper.eq("is_delete", 0);
        List<Role> roleList = iRoleService.list(queryWrapper);
        if(roleList.size() > 0){
            return RestUtil.rest(ServiceCode.CODE_403, "该角色已存在");
        }

        Claims claims = JwtUtil.parseJWT(request.getHeader("token"));
        Integer user_id = claims.get("user_id", Integer.class);

        role.setInputUserId(user_id);
        iRoleService.save(role);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "updateRole", method = RequestMethod.POST)
    @ResponseBody
    public Rest updateRole(@RequestBody Role role){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", role.getName());
        queryWrapper.eq("is_delete", 0);
        List<Role> roleList = iRoleService.list(queryWrapper);
        for (int i=0; i<roleList.size(); i++){
            if (roleList.get(i).getId().equals(role.getId())){
                continue;
            }
            return RestUtil.rest(ServiceCode.CODE_403, "该角色已存在");
        }

        iRoleService.updateById(role);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "deleteRole", method = RequestMethod.POST)
    @ResponseBody
    public Rest deleteRole(Integer id){
        Role role = new Role();
        role.setId(id);
        role.setIsDelete(1);

        iRoleService.updateById(role);

        return RestUtil.rest(ServiceCode.CODE_200, "成功");
    }

    @ApiOperation(value = "分配权限")
    @RequestMapping(value = "assignPermission", method = RequestMethod.POST)
    @ResponseBody
    public Rest assignPermission(@RequestBody RoleDTO roleDTO ){
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleDTO.getId());

        iRolePermissionService.remove(queryWrapper);

        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Integer permissionId: roleDTO.getPermissionIds()){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleDTO.getId());
            rolePermission.setPermissionId(permissionId);

            rolePermissionList.add(rolePermission);
        }

        iRolePermissionService.saveBatch(rolePermissionList);

        QueryWrapper<RoleVillage> roleVillageWrapper = new QueryWrapper<>();
        roleVillageWrapper.eq("role_id", roleDTO.getId());
        iRoleVillageService.remove(roleVillageWrapper);

        List<RoleVillage> roleVillageList = new ArrayList<>();
        for (RoleVillageDTO roleVillageDTO: roleDTO.getRoleVillageDTOList()){
            RoleVillage roleVillage = new RoleVillage();
            roleVillage.setRoleId(roleDTO.getId());
            roleVillage.setVillageId(roleVillageDTO.getVillageId());
            roleVillage.setScope(roleVillageDTO.getScope());

            roleVillageList.add(roleVillage);
        }
        iRoleVillageService.saveBatch(roleVillageList);

        return RestUtil.rest(ServiceCode.CODE_200, "成功");
    }

}

